<?php
require_once( 'lib/fpdf/tfpdf.php' );
require_once( 'database-helper.php' );
require_once( 'device-manager.php' );
require_once( 'rent-manager.php' );

class ContractPDF extends TFPDF {
	private $total_sum = 0;

	// Page header
	function Header() {
		// Logo
	    $this->Image('img/logo.png',23,15, 80);
		// Arial bold 15
		$this->SetFont('Times','',12);
		// Line break
		$this->Ln(30);
	}
	
	function getTotalSum() {
		return $this->total_sum;
	}

	// Better table
	function ImprovedTable( $devices ) {
	
		$header = array('Nimetus', 'Seerianumber / IMEI', 'Väärtus', 'Vastuvõetud');
		// Column widths
		$w = array(60, 39, 30, 30);
		// Header
		$this->Ln(9);
		for($i=0;$i<count($header);$i++) {
			$this->Cell($w[$i],7,$header[$i],1,0,'C');
		}

		$this->Ln();
		
		foreach( $devices as $device ) {
			$this->Cell( $w[0], 6, $device->getModel(), 'LR', 0, 'L' );
			$this->Cell( $w[1], 6, $device->getSerial(), 'LR', 0, 'C') ;
			$this->Cell( $w[2], 6, $device->getRawValue().' €', 'LR' ,0, 'R');
			$this->Cell( $w[3], 6, '', 'LR', 0, 'R');
			$this->Ln();
			$this->total_sum += $device->getRawValue();
		}
		
		$x = array_sum($w) - $w[3];
		
		// Closing line
		$this->Cell(array_sum($w),0,'','T', 1);
		$this->setX( $x );
		$this->Cell( $w[2], 6, $this->getTotalSum().' €', 'LR', 1, 'R');
		$this->setX( $x );
		$this->Cell( $w[2], 0,'','T', 1);
		$this->Ln();
		$this->Ln(7);
	}
	
	// Page footer
	function Footer() {
		// Position at 1.5 cm from bottom
		$this->SetY(-15);
		// Arial italic 8
		$this->SetFont('Times','',8);
		// Column widths
		$w = array(20, 25, 23, 25);
		
		$data = array( array( 'tel 472 0240', 'kolledz@hk.tlu.ee', 'Lihula mnt 12', 'Reg kood 7400122'), array( 'faks 472 0248', 'www.hk.tlu.ee', '90507 Haapsalu', 'Konto 10602009155001') );
		$this->setTextColor( 100 );
		foreach( $data as $row ) {
			$this->Cell( $w[0], 4, $row[0], 'R' );
			$this->Cell( 2 );
			$this->Cell( $w[1], 4, $row[1], 'R' ) ;
			$this->Cell( 2 );
			$this->Cell( $w[2], 4, $row[2], 'R' );
			$this->Cell( 2 );
			$this->Cell( $w[3], 4, $row[3]);
			$this->Ln();
		}
	}
}

if ( isSet( $_GET['id'] ) ) {
	$rentManager = new RentManager();
	$contract = $rentManager->getContractByID( $_GET['id'] );
	if ( $contract ) {
		$user = $contract->getUser();
		
		// Instanciation of inherited class
		$pdf = new ContractPDF();
		$pdf->AddFont('Times','','Times.ttf',true);
		
		$pdf->setCreator( 'HK-RENT', true );
		$pdf->setAuthor( 'Tallinna Ülikooli Haapsalu Kolledž', true );
		$pdf->setTitle( 'Seadme(te) laenuleping', true );
		$pdf->setMargins( 30, 25, 20 );
		$pdf->setDrawColor( 100 );
		$pdf->AddPage('P', 'A4');
		$pdf->SetFont('Times','',12);
		
		$pdf->Cell(0, 7, 'Lepingu nr '.$contract->getStringID(), 0, 1, 'R');
		
		$pdf->Cell(0, 7, 'Nimi: '.$user->getFullname(), 0, 1);
		$pdf->Cell(0,7,'E-post: '.$user->getEmail(), 0, 1);
		$pdf->Cell(0,7,'Tel: '.$user->getPhone(), 0, 1);
		$pdf->Ln(7);
		$pdf->Cell(127,7, 'Seadme(te) laenuleping');
		$pdf->Cell(0,7, $contract->getDate(), 0, 1);

		
		$pdf->ImprovedTable( $contract->getDevices() );
		
				
		$pdf->MultiCell(0,7,'Olen teadlik, et minu kasutuses oleva(te) seadme(te) koguväärtus on '.$pdf->getTotalSum().' eurot. Olen tutvunud seadme(te) tööpõhimõtetega. Vastutan seadme(te) eest ja kohustun '.(count($contract->getDevices()) == 1 ? 'selle' : 'need').' isiklikult tagastama Tallinna Ülikooli Haapsalu Kolledži IT-osakonda kokkulepitud tähtajal. Kui lõhun või kaotan minu  kasutuses oleva(d) seadme(d) kohustun maksma kahjutasu Tallinna Ülikooli Haapsalu Kolledžile.');
		
		$pdf->Ln(5);

		$pdf->Cell(0,7,'Võtan seadme(d) kasutusele: '.$contract->getDate(), 0, 1);
		$pdf->Cell(116,7,'Tagastan seadme(d) IT-osakonda: '.$contract->getReturnDate(), 0, 1);
		$pdf->Cell(0,15,'Allkiri: _____________', 0, 1, 'R');
		$pdf->SetFont('Times','',8);
		$pdf->setX( 166 );
		$pdf->Cell(0,-7,'(laenule võtja)', 0, 1);
		
		$pdf->SetFont('Times','',12);
		$pdf->Cell(0,15,'Tagastatud IT-osakonda: _____________', 0, 1);
		
		$pdf->SetFont('Times','',8);
		$pdf->setX( 81 );
		$pdf->Cell(0,-7,'(kuupäev)', 0, 1);
		
		$pdf->Output($contract->getPDFName(), 'I');
		return;
	}
}
echo 'Hei, ei leidnud lepingut!';
?>